package com.future.hedongjun.home.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dl7.player.media.IjkPlayerView;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.utils.ToastUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.home.adapter.ClassHourAdapter;
import com.future.hedongjun.home.bean.ClassHourBean;
import com.future.hedongjun.home.bean.response.ClassHourResponse;
import com.future.hedongjun.home.bean.response.ClassIntroduceResponse;
import com.future.hedongjun.mine.ui.LoginActivity;
import com.future.hedongjun.mine.utils.RoundProgress;

import java.io.File;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 创建日期：2018/11/19 17:57
 * author liu.p.f
 * 类说明： 课时
 */
public class ClassHourFragment extends Fragment {


    @BindView(R.id.recycler_classHour)
    RecyclerView recyclerClassHour;
    private int mLastCheckedPosition = -1;
    private ClassHourAdapter classHourAdapter;
    private SparseBooleanArray mBooleanArray;
    private IjkPlayerView mIjkPlayer;
    // private ArrayList<ClassHourBean> beans;
    private String ClassId;
    private List<ClassHourBean> classHourBeans;
    Unbinder unbinder;

    public ClassHourFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hour, container, false);
        unbinder = ButterKnife.bind(this, view);
        initFile();
        getData();
        return view;
    }

    private void initView() {
        classHourAdapter = new ClassHourAdapter(classHourBeans,ClassId);
        View headerView = getLayoutInflater().inflate(R.layout.recycler_item_class_hour_head, null);
        headerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        classHourAdapter.addHeaderView(headerView);
        TextView tvClassNum = headerView.findViewById(R.id.tv_classNum);
        TextView tvClassDown = headerView.findViewById(R.id.tv_classDown);
        TextView tvProgress = headerView.findViewById(R.id.tv_progress);
        tvClassNum.setText("全部课程(" + classHourBeans.size() + ")");
        classHourAdapter.setSelectData(mBooleanArray, mIjkPlayer, tvClassDown, tvProgress);
        recyclerClassHour.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerClassHour.setAdapter(classHourAdapter);
    }


    public void setIjkVideo(IjkPlayerView ijkDetailPlayer, String classId) {
        this.mIjkPlayer = ijkDetailPlayer;
        this.ClassId = classId;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void getData() {
        if (MyApp.getInstance().getUserToken().isEmpty()) {
            startActivity(new Intent(getContext(), LoginActivity.class));
        } else {
            new HttpRequest<ClassHourResponse>()
                    .with(getContext())
                    .setApiCode("_classdetail_001")
                    .addParam("id", ClassId)
                    .addParam("userToken", MyApp.getInstance().getUserToken())
                    .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                        @Override
                        public void onSuccess(BaseResponse response) {
                            classHourBeans = ((ClassHourResponse) response).data;
                            //视频文件
                            String fileVideoItem = Environment.getExternalStorageDirectory().toString() + File.separator + "CacheFile" + "/" + "CacheVideo" + "/" + classHourBeans.get(0).ClassName;
                            File fileItem = new File(fileVideoItem);
                            if (!fileItem.exists())
                                fileItem.mkdirs();

                            //音频文件
                            String fileVoiceItem = Environment.getExternalStorageDirectory().toString() + File.separator + "CacheFile" + "/" + "CacheVoice" + "/" + classHourBeans.get(0).ClassName;
                            File fileVoice = new File(fileVoiceItem);
                            if (!fileVoice.exists())
                                fileVoice.mkdirs();

                            //文档文件
                            String fileDocItem = Environment.getExternalStorageDirectory().toString() + File.separator + "CacheFile" + "/" + "CacheDoc" + "/" + classHourBeans.get(0).ClassName;
                            File fileDoc = new File(fileDocItem);
                            if (!fileDoc.exists())
                                fileDoc.mkdirs();

                            mBooleanArray = new SparseBooleanArray(classHourBeans.size());
                            initView();
                        }
                    }).start(new ClassHourResponse());
        }
    }


    private void initFile() {
        String url = Environment.getExternalStorageDirectory().toString() + File.separator + "CacheFile";
        File file = new File(url);
        if (!file.exists())
            file.mkdirs();
        String fileVideoUrl = Environment.getExternalStorageDirectory().toString() + File.separator + "CacheFile" + "/" + "CacheVideo";
        File fileVideo = new File(fileVideoUrl);
        if (!fileVideo.exists())
            fileVideo.mkdirs();

        String fileVoiceUrl = Environment.getExternalStorageDirectory().toString() + File.separator + "CacheFile" + "/" + "CacheVoice";
        File fileVoice = new File(fileVoiceUrl);
        if (!fileVoice.exists())
            fileVoice.mkdirs();

        String fileDocUrl = Environment.getExternalStorageDirectory().toString() + File.separator + "CacheFile" + "/" + "CacheDoc";
        File fileDoc = new File(fileDocUrl);
        if (!fileDoc.exists())
            fileDoc.mkdirs();
    }
}
